<?php
namespace Home\Controller;
use Think\Controller;
header('content-type:text/html;charset=utf-8');

class UserController extends Controller{
		
	//查询用户信息
	public function index(){
	//User表和User_detail表
	$user=D('User');
	$id=session('id');
	$user=$user->relation(true)->where('id='.$id)->find();

    $this->assign('user',$user);
    $this->display();
    }


     //取消订单
    public function off(){
        $order_number=I('get.order_number');

       $order=M('orders');

       $order->create();
       $orders=$order->where('order_number='.$order_number)->data('state=取消订单')->save();

       if($orders){
            $this->success('订单取消成功',''.__CONTROLLER__.'/index/order_number/'.$order_number.'');
        }else{
            $this->error('订单取消失败',''.__CONTROLLER__.'/index/order_number/'.$order_number.'');
        }
    }


    public function mod(){
    	//查询所有信息
    	$user=D('User');
    	$id=session('id');
    	$data=$user->field('a.*,b.name,b.areaid,b.adresse,b.sex,b.addtime,b.face,b.tel')
            ->alias('a')
            ->join('LEFT JOIN yg_user_detail as b on a.id=b.id')
            ->where("a.id=$id")
            ->select();
        //$data=$user->relation(true)->where('id='.$id)->select();
        //dump($user_detail->_sql());

    	$this->assign('user',$data);
    	$this->display();
    }


    public function update(){
        //修改用户基本资料
        if ($_FILES['face']['name']) {

            $upload = new \Think\Upload();
            // 实例化上传类
            $upload->rootPath = './Public/Upload/';
            $upload->savePath = '';
            $upload->maxSize = 2 * 1024 * 1024;
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
            $upload->saveName = time() . '_' . mt_rand();
            // 设置附件上传大小
            $result = $upload->upload();

            if (!$result) {
                $this->error('上传文件失败，失败信息为:' . $upload->getError(), U('User/mod'));
            }
            //接受上传信息
            $_POST['face'] = $result['face']['savepath'] . $result['face']['savename'];
        }
        //插入数据库
        $_POST['addtime']=date('Y-m-d H:i:s',time());
        $_POST['areaid'] = $_POST['province'].'-'.$_POST['city'].'-'.$_POST['county'].'-'.$_POST['xiang'];
        unset($_POST['province']);
        unset($_POST['city']);
        unset($_POST['county']);
        unset($_POST['xiang']);

        $user=M('user_detail');
        $id=session('id');
        $user->create();
        $res=$user->where('id='.$id)->save($_POST);
        if($res){
            $this->success('修改成功',U('User/mod'));
        }else{
            $this->error('修改失败',U('User/mod'),5);
        }
    }

    //修改密码
    public function pass(){
        $id=session('id');
        if (!empty($id)) {
            $id = session('id');
            //exit;
        }else{
            $id = I('post.uid');
        }
        //dump($id);
//        验证两次密码是否一致
        if(I('post.password')==I('post.repassword')){
            $user=M('User');
            $_POST['password'] = md5($_POST['password']);
            $user->create();

            if($user->where('id='.$id)->save()){
                //判断修改密码的入口
                if(session('id')){
                    $this->success('修改密码成功！',U('User/index'));
                }else{
                    $this->success('修改密码成功！',U('Login/index'));
                }
                //修改成功 跳转到用户中心

            }else{
                $this->error('修改失败，请重新修改',U('User/pwd'));
            }
        }else{
            //跳转回修改密码页面即可
            $this->error('两次密码不一致，请重新修改',U('User/pwd'));

        }
    }


    public function area()
    {
        // 1.实例化Model
        $area = M('area');

        // 2.查询数据（只查询省份）
        $upid = I('get.upid', 0);
        $data = $area->where('upid='.$upid)->select();
        // dump($data);

        // 3.返回结果
        $this->ajaxReturn($data);
    }
    public function email(){
        $email = I('post.email');
        $user = M('user');
        $res = $user->where("email='$email'")->find();
        if(!empty($_POST)) {
            if ($res) {
                $id=$res['id'];
                redirect("onemail/id/$id");
            }else{
                $this->error('没有该邮箱',U('User/email'));
            }
        }
        $this->assign('user',$res);
        $this->display();
    }
    // 邮箱验证
    public function doemail(){
	    /*
	     * 1.查询数据库是否有该邮箱
	     * 2.跳转到制定界面
	     * 3.更改完成*/
        $email = I('post.email');
        $JSONObject = array("code" => 0, "message" => "");
        $user=M('User');
        //print_r($email);
        $res = $user->where("email='$email'")->find();
        if(!empty($_POST)) {
            if ($res) {
                $JSONObject['code'] =1;
                exit(json_encode($JSONObject));
            }else{
                $JSONObject['message'] = "该邮箱不存在";
                exit(json_encode($JSONObject));
            }
        }
       }
    public function onemail()
    {
        //导入文件
        vendor('PHPMailer.PHPMailerAutoload');
        $id = I("get.id");
//        dump($id);
        $user=M('User');
        $res =$user->where('id='.$id)->find();
        if(think_send_mail($res['email'], $res['username'], $subject = '重置密码', $body = "
					尊敬的客户：<br/>您已请求了重置密码。<br/>请点击下面的连接来重置密码。<br/>
   				<a href='http://localhost/php/bookshop/index.php/Home/User/pwd/id/$id'
    				target='_blank'>点击这里重置</a><br/>")){
        }
   }

}